package com.hzy.demo.DataService;

import com.hzy.demo.models.RegionStats;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Service
public class DataServices implements InitializingBean {
    private static final String urlTemplate= "https://gitee.com/voghost/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";

    private final List<RegionStats> allRegionStats = new ArrayList<>();

//    DataServices() throws IOException{
//        fetchCoronVirusData();
//    }

    public List<RegionStats> getAllRegionStats() { return allRegionStats; }

    @Scheduled(cron = "0 0 1 * * *")
    public void fetchCoronVirusData() throws IOException{
        RequestEntity<Void> requestEntity = RequestEntity
                .get(URI.create(urlTemplate))
                .headers(httpHeaders -> httpHeaders.add("User-Agent","Mozilla/5.0"))
                .build();
        ResponseEntity<Resource> exchange = new RestTemplate().exchange(requestEntity,Resource.class);
        Resource body = exchange.getBody();

        final String[] FILE_HEADER = {"Province/State","Country","lat","long"};
        CSVFormat format = CSVFormat.DEFAULT.withHeader(FILE_HEADER).withSkipHeaderRecord();
        assert body != null;
        Reader in =new InputStreamReader(body.getInputStream());
//        Reader in =new FileReader(pathTemp+"/"+"CoronaVirus.csv");

        Iterable<CSVRecord> records=format.parse(in);
        for (CSVRecord record: records){
            RegionStats regionStats=new RegionStats();

            regionStats.setState(record.get(0));
            regionStats.setCountry(record.get(1));
            regionStats.setLatestTotalCases(Integer.parseInt(record.get(record.size()-1)));
            regionStats.setDiffFromPrevDay(Integer.parseInt(record.get(record.size()-1))-Integer.parseInt(record.get(record.size()-2)));
            allRegionStats.add(regionStats);
        }
//        Reader reader = new InputStreamReader(body.getInputStream());
//        Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(reader);
//        for(CSVRecord record : records){
//            String state = record.get("state");
//            String country = record.get("country");
//            String latestTotalCases = record.get("latestTotalCases");
//            String diffFromPrevDay = record.get("diffFromPrevDay");
//        }
    }

    public void afterPropertiesSet() throws Exception{
        fetchCoronVirusData();
    }
}
